/***************************************************************************
 *   MODULE NAME :
 *  PROGRAM NAME : -c
 *        AUTHOR : HOTMOCHA
 *  CREATED DATE : 2015-01-13 18:51:21
 *  LIBRARY NAME :
 *  PROGRAM DESC :
 *
 *       HISTORY :
 *
 **************************************************************************/
#include "simplebuf.h"
void FreeBuf(Buf *buf)
{
	if (buf) {
		if (buf->base)
			free(buf->base);
		free(buf);
	}
}

void FreeData(Buf *buf)
{
	if (buf) {
		if (buf->base)
			free(buf->base);
		buf->usedlen = 0;
		buf->totallen = 0;
	}
}
Buf* InitBuf(unsigned long len)
{
	Buf *buf = NULL;
	if (len < 0)
		return NULL;
	//初始化肯定有长度的
	if (len == 0)
		len = DEFAULTINITBUFLEN;
	buf = (Buf*)malloc(sizeof(Buf));
	if (buf == NULL)
		return NULL;
	buf->totallen = buf->usedlen = 0;
	buf->base = NULL;
	if (len == 0)
		return buf;
	else
	{
		buf->base = (char*)malloc(len);
		if (buf->base == NULL)
		{
			FreeBuf(buf);
			return NULL;
		}
		buf->totallen = len;
		buf->usedlen = 0;
		memset(buf->base, 0x00, len);
		return buf;
	}
}
void ReinitBuf(Buf *buf)
{
	if (buf == NULL)
		return;
	if (buf->base == NULL)
		return;
	memset(buf->base, 0x00, buf->totallen);
	buf->usedlen = 0;
}
//会修改totallen的长度
int Realloc(Buf* buf, unsigned long addlen)
{
//printf("used [%ld], total [%ld] addlen [%ld]\n", buf->usedlen, buf->totallen, addlen);
//printf("===================realloc=======\n");
//printf("%s\n", buf->base);
	char *p = NULL;
	if (buf == NULL)
		return BUF_ARGNULL_ERR; 
	p = (char*)realloc(buf->base, buf->totallen + addlen * 2 );
	if (p == NULL)
		return BUF_MEMMALLOC_ERR;
	buf->base = p;
	buf->totallen += addlen * 2;	
//printf("===================realloc end=======\n");
	return 0;	
}	
int Appendmsg(Buf* buf, char *msg, unsigned long msglen)
{
	int ret = 0;
	long addlen = 0;
	if (buf == NULL)	return BUF_ARGNULL_ERR;
	if (msg == NULL)	return 0;
//printf("used [%ld], total [%ld] msglen [%ld]\n", buf->usedlen, buf->totallen, msglen);
	addlen = msglen - (buf->totallen - buf->usedlen);	
	if (addlen > 0)	
	{
		ret = Realloc(buf, addlen);
		if (ret)
			return BUF_MEMMALLOC_ERR;
	}
//	sprintf(buf->base + buf->usedlen, "%s", msg );
	memcpy(buf->base + buf->usedlen, msg, msglen );
	buf->usedlen += msglen;
	return 0;
}
int Setmsg(Buf *buf, char *msg, unsigned long msglen) 
{
	int ret = 0;
	int addlen = 0;
	if (buf == NULL)	return BUF_ARGNULL_ERR;
	if (msg == NULL) {
		buf->usedlen = 0;
		if (buf->base) {
			buf->base[0] = '\0';
		}
	}
	else {
		addlen = msglen - buf->totallen;
		if (addlen > 0) {
			ret = Realloc(buf, addlen);
			if (ret)
				return BUF_MEMMALLOC_ERR;
		}
		memset(buf->base, 0x00, buf->totallen);
		memcpy(buf->base, msg, msglen);
		buf->usedlen = msglen;
	}
	return 0;
}
Buf* InitBufWithMsg(char *msg, unsigned long msglen)
{
	int ret = 0;
	Buf *buf = NULL;
	buf = InitBuf(msglen * 2);
	if (!buf)
		return NULL;
	memcpy(buf->base, msg, msglen);
	buf->usedlen = msglen;
	buf->totallen = msglen * 2;
	return buf;
}
Buf* DupBuf(Buf *in)
{
	if (!in)
		return NULL;

	Buf *buf = (Buf*)malloc(sizeof(Buf));
	if (!buf)
		return NULL;
	if (in->totallen == 0)
		return buf;
	buf->base = (char*)malloc(sizeof(in->totallen));
	buf->usedlen = in->usedlen;
	buf->totallen = in->totallen;
	return buf;
}
